package com.tourism.www;

import java.io.IOException;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.t2cn.common.www.ServletCommon;
import com.t2cn.util.StringUtil;
import com.tourism.storage.dao.CourseDao;
import com.tourism.storage.dao.PartnerDao;
import com.tourism.storage.dao.PassengersDao;
import com.tourism.storage.dao.TeamsDao;
import com.tourism.storage.dto.PassengersDto;
import com.tourism.storage.dto.TeamsDto;
import com.tourism.storage.pojo.Courses;
import com.tourism.storage.pojo.Partner;
import com.tourism.storage.pojo.Passengers;
import com.tourism.storage.pojo.Teams;
import com.tourism.www.common.TourismBaseHandler;

public class TeamsHandler extends TourismBaseHandler {

	@Override
	public String doActionX(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		
		String method = getMethodName(request);
		if("page_add".equals(method)){
			return toAddPage(request, response);
		}else if("add".equals(method)){
			return addTeams(request, response);
		}else if("list".equals(method)){
			return list(request, response);
		}else if("delete".equals(method)){
			return delete(request, response);
		}else if("uStatus".equals(method)){
			return updateStatus(request, response);
		}else if("test".equals(method)){
			return "test";
		}else if("getPassengers2T".equals(method)){
			return getPassengersJoinTeam(request, response);
		}else if("uPassengersTeam".equals(method)){
			return uPassengersTeam(request, response);
		}else if("addTeamPassengers".equals(method)){
			return addTeamPassengers(request, response);
		}else if("deleteTeamPassengers".equals(method)){
			return deleteTeamPassengers(request, response);
		}

		return null;
	}

	private String toAddPage(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException{
		CourseDao courseDao = new CourseDao();
		List<Courses> coursesList = courseDao.getCourses(0);
		request.setAttribute("coursesList", coursesList);

System.out.println("-=-=-=-=-=-=-=-=-=toAddPage-=-=-=-=-=-=-=-=-=");
		return "page_add";
	}	
	private String addTeams(HttpServletRequest request, 
			HttpServletResponse response) throws ServletException, IOException {

		String createdate = ServletCommon.getParamValue(request, "createdate");
		Integer courseid = ServletCommon.getIntParamValue(request, "courseid", 0);
		Integer maxpassengers = ServletCommon.getIntParamValue(request, "maxpassengers", 0);
		Integer repassengers = ServletCommon.getIntParamValue(request, "repassengers", 0);	
//		Integer status = ServletCommon.getIntParamValue(request, "status", 0);
		
		Teams	pojo = new Teams();
		
		pojo.setCreatedate(new Timestamp(StringUtil.toYyyyMmDdDate(createdate, new Date()).getTime()));
		pojo.setCourseid(courseid);
		pojo.setMaxpassengers(maxpassengers);
		pojo.setRepassengers(repassengers);
		pojo.setStatus(0);
		
		TeamsDao dao = new TeamsDao();
		dao.addTeams(pojo);
		
		return "list";
	}
	
	private TeamsDto Pojo2Dto(Teams pojo){
		TeamsDto dto = new TeamsDto();
		dto.setTeams(pojo);

		CourseDao courseDao = new CourseDao();
		dto.setCoursename(courseDao.getCourseById(pojo.getCourseid()).getCoursename());
		dto.setTeamcode("T" + courseDao.getCourseById(pojo.getCourseid()).getCoursecode() + dto.getTeams().getTeamid());
		
		PassengersDao passengersDao = new PassengersDao();
		dto.setNowpassengers(passengersDao.getTeamPassengers(pojo.getTeamid()));
		
		return dto;	
	}
	
	private String list(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException{
		Teams pojo = new Teams();
		Integer status = ServletCommon.getIntParamValue(request, "status", 0);
		//where.setStatus(0);
		System.out.println("-=-=-=-=-=-=-=-=-=Teams list:status:"+status);			
		TeamsDao teamsDao = new TeamsDao();
		List<Teams> list = teamsDao.getTeams(status);
		
		List<TeamsDto> dto_list = new ArrayList<TeamsDto>();
		
		for(int i=0; i < list.size(); i++){
			TeamsDto dto = new TeamsDto();
			dto = Pojo2Dto(list.get(i));
			dto_list.add(dto);
		}
		request.setAttribute("teamsDto", dto_list);
		return "page_list";
	}
	
	private String getPassengersJoinTeam(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException{
		Integer teamid = ServletCommon.getIntParamValue(request, "teamid", 0);
		
		TeamsDao teamsDao = new TeamsDao();
		TeamsDto dto = new TeamsDto();
		Teams pojo = teamsDao.getTeamsById(teamid);
		dto = Pojo2Dto(pojo);
		
		PassengersDao pdao = new PassengersDao();
		Passengers passengers = new Passengers();
		passengers.setTeamid(teamid);
		List<PassengersDto> plist = pdao.getTeamPassengersesDto(passengers);
	
		request.setAttribute("teamsDto", dto);
		request.setAttribute("passengers", plist);
		
		return "team_pa_list";
	}
	
	private String addTeamPassengers(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException{
		Integer teamid = ServletCommon.getIntParamValue(request, "teamid", 0);		
		String contact = ServletCommon.getParamValue(request, "contact");
		Integer adults = ServletCommon.getIntParamValue(request, "adults", 0);
		Integer childs = ServletCommon.getIntParamValue(request, "childs", 0);
//		Integer courseid = ServletCommon.getIntParamValue(request, "courseid", 0);
//		String departuredate = ServletCommon.getParamValue(request, "departuredate");
		String phone = ServletCommon.getParamValue(request, "phone");
		String fax = ServletCommon.getParamValue(request, "fax");
		Integer price = ServletCommon.getIntParamValue(request, "price", 0);
		Integer paymode = ServletCommon.getIntParamValue(request, "paymode", 0);	
		Integer paid = ServletCommon.getIntParamValue(request, "paid", 0);
		Integer ifrom = ServletCommon.getIntParamValue(request, "ifrom", 0);
		
		Passengers	pojo = new Passengers();
		PassengersDao dao = new PassengersDao();
		TeamsDao teamsDao = new TeamsDao();
		TeamsDto teamsDto = new TeamsDto();
		Teams teams = teamsDao.getTeamsById(teamid);
		
		System.out.println("-=-=-=-=-=-=-=-=-=addTeamPassengers:teamid:"+teamid);	
		
		Integer courseid = teams.getCourseid();
		String departuredate = teams.getCreatedate().toString().substring(0,10);
		
		pojo.setAdults(adults);
		pojo.setChilds(childs);
		pojo.setContact(contact);
		pojo.setCourseid(courseid);
		pojo.setDeparturedate(new Timestamp(StringUtil.toYyyyMmDdDate(departuredate, new Date()).getTime()));
		pojo.setFax(fax);
		pojo.setIfrom(ifrom);
		pojo.setPaid(paid);
		pojo.setPaymode(paymode);
		pojo.setPhone(phone);
		pojo.setPrice(price);
		pojo.setTeamid(teamid);
		pojo.setStatus(0);	
		
		dao.addPassengers(pojo);		
		
		teamsDto = Pojo2Dto(teams);
		
		PassengersDao pdao = new PassengersDao();
		Passengers passengers = new Passengers();
		passengers.setTeamid(teamid);
		List<PassengersDto> plist = pdao.getTeamPassengersesDto(passengers);
	
		request.setAttribute("teamsDto", teamsDto);
		request.setAttribute("passengers", plist);
		
		return "team_pa_list";
	}
	
	private String deleteTeamPassengers(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException{
		Integer infoid = ServletCommon.getIntParamValue(request, "infoid", 0);
		Integer teamid = ServletCommon.getIntParamValue(request, "teamid", 0);		
		
		PassengersDao dao = new PassengersDao();
		TeamsDao teamsDao = new TeamsDao();
		TeamsDto teamsDto = new TeamsDto();
		Teams teams = teamsDao.getTeamsById(teamid);
		
		System.out.println("-=-=-=-=-=-=-=-=-=deleteTeamPassengers:infoid:" + infoid + " teamid:" + teamid);	
		
		dao.deletePassengers(infoid);	
		
		teamsDto = Pojo2Dto(teams);
		
		PassengersDao pdao = new PassengersDao();
		Passengers passengers = new Passengers();
		passengers.setTeamid(teamid);
		List<PassengersDto> plist = pdao.getTeamPassengersesDto(passengers);
	
		request.setAttribute("teamsDto", teamsDto);
		request.setAttribute("passengers", plist);
		
		return "team_pa_list";
	}
	
	private String delete(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException{
		Integer teamid = ServletCommon.getIntParamValue(request, "teamid", 0);

		TeamsDao dao = new TeamsDao();
		dao.deleteTeams(teamid);
		
		return "list";
	}
	
	private String updateStatus(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException{
		Integer teamid = ServletCommon.getIntParamValue(request, "teamid", 0);
		Integer status = ServletCommon.getIntParamValue(request, "status", 0);

		System.out.println("-=-=-=-=-=-=-=-=-=teamid:" + teamid);
		System.out.println("-=-=-=-=-=-=-=-=-=status:" + status);
		if(status == 0)
			status = 1;
		else
			status = 0;
		Teams pojo = new Teams();
		pojo.setTeamid(teamid);
		pojo.setStatus(status);
		TeamsDao dao = new TeamsDao();
		dao.updateTeams(pojo);
		
		return "list";
	}
	
	private String uPassengersTeam(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException{
		Integer infoid = ServletCommon.getIntParamValue(request, "infoid", 0);
		Integer teamid = ServletCommon.getIntParamValue(request, "teamid", 0);

		Passengers pojo = new Passengers();
		pojo.setInfoid(infoid);
		pojo.setTeamid(teamid);
		PassengersDao dao = new PassengersDao();
		dao.updatePassengers(pojo);
		
		TeamsDao teamsDao = new TeamsDao();
		TeamsDto dto = new TeamsDto();
		Teams teams = teamsDao.getTeamsById(teamid);
		dto = Pojo2Dto(teams);
		
		PassengersDao pdao = new PassengersDao();
		Passengers passengers = new Passengers();
		passengers.setTeamid(0);
		passengers.setCourseid(dto.getTeams().getCourseid());
		List<PassengersDto> plist = pdao.getTeamPassengersesDto(passengers);
	
		request.setAttribute("teamsDto", dto);
		request.setAttribute("passengers", plist);		
		
		return "team_pa_list";
	}
}
